//
//  RadioListTableViewController.m
//  Slice
//
//  Created by lanou on 16/6/17.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "RadioListTableViewController.h"

@interface RadioListTableViewController ()

@end

@implementation RadioListTableViewController

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)getData {
    [RequestTool requsetWithType:POST URLString:kRadioDetailURl paremeter:@{@"radioid":self.radioID} callBack:^(NSData *data, NSError *error) {
        if (data == nil) {
            return ;
        }
        NSMutableArray *mArr = [NSMutableArray array];
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSArray *arr = jsonDic[@"data"][@"list"];
        for (NSDictionary *dic12 in arr) {
            [self.dataArray addObject: [RadioDetailModel modelWithDiction:dic12]];
        }
      
        for (NSString *string in jsonDic[@"data"][@"radioInfo"]) {
            if ([string isEqualToString:@"coverimg"]) {
                NSString *strURl =  [jsonDic[@"data"][@"radioInfo"] objectForKey:string];
                
                [mArr addObject: strURl];
            }
        }
        
        
        
     dispatch_async(dispatch_get_main_queue(), ^{
         if (self.tableView.tableHeaderView == nil) {
            CoueousPic *header = [[CoueousPic alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight - kWidth - 69) images:mArr];
             self.tableView.tableHeaderView = header;
             self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                 [self getMoreData];
             }];
         }
         [self.tableView reloadData];
     });
        
    }];
}

- (void)getMoreData {
    [RequestTool requsetWithType:POST URLString:kReadMoreDetailURL paremeter:@{@"radioid":self.radioID,@"start":@(self.dataArray.count)} callBack:^(NSData *data, NSError *error) {
        if (data == nil) {
            return ;
        }
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSArray *arr = jsonDic[@"data"][@"list"];
        for (NSDictionary *dic12 in arr) {
            [self.dataArray addObject: [RadioDetailModel modelWithDiction:dic12]];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
        });
        
    }];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.name;
    
    [self getData];
    
    // Uncomment the following line to preserve selection between presentations.
     self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.tableView registerNib:[UINib nibWithNibName:@"RadioDetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"123"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   RadioDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"123" forIndexPath:indexPath];
   
      [cell getDataByModel:self.dataArray[indexPath.row]];
    [cell removeFromSuperview];
    [cell.pauseAndStartBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    //将上面的钢化膜隐藏掉
    cell.contentView.hidden = YES;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    RadioDetailViewController *vc = [[RadioDetailViewController alloc] init];
    
    vc.musicList = self.dataArray;
    
    
    vc.selectIndex = indexPath.row;
    
    RadioDetailModel *model = self.dataArray[indexPath.row];
    
    [[MusicManager share] playMusicWithStringURL:model.musicUrl];
    
    [self.navigationController pushViewController:vc animated:YES];
}

//button 的动作事件
- (void)btnAction:(UIButton *)btn {
//    通过button获得cell
    UITableViewCell *cell1 = (UITableViewCell *)btn.superview;
//    通过cell获取点击的下标
    NSIndexPath *index = [self.tableView indexPathForCell:cell1];
    RadioDetailModel *model1 = self.dataArray[index.row];
    [[MusicManager share] playMusicWithStringURL:model1.musicUrl];
}















/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
